package com.ihealthway.all.groupbuy.modules.admin;

import com.ihealthway.all.slog.annotation.SLog;
import com.ihealthway.all.commons.Constants;
import com.ihealthway.all.groupbuy.services.RewardInfoService;
import com.ihealthway.all.utils.Ajax;
import com.ihealthway.all.utils.AjaxReturn;
import com.ihealthway.all.utils.Utils;
import com.ihealthway.all.groupbuy.vo.Reward;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Files;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * reward info module class
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@IocBean
@At("/admin/reward")
public class RewardInfoModule {

    private static final Log logger = Logs.get();

    @Inject("conf")
    private PropertiesProxy props;

    @Inject
    private RewardInfoService rwdInfoService;

    @At
    @Ok("beetl:admin/rewardinfo.jsp")
    public void index(HttpServletRequest req) {

    }

    @POST
    @At
    @AdaptBy(type = UploadAdaptor.class, args = { "ioc:uploadImg" })
    @SLog(module = Constants.MODULE_SHOP, type=Constants.OPERATE_TYPE_ADD, content="新增【${args[0].rewardName}】奖品信息")
    public AjaxReturn addNew(@Param("..") Reward reward, @Param("rewardImg")TempFile rewardImg) {
        AjaxReturn ar = Ajax.ok();

        uploadRewardImg(reward, rewardImg);

        boolean flag = (rwdInfoService.add(reward)!=null);
        if (!flag) {
            ar.setOk(false);
            ar.setMsg("Add new order failure!!!");
        }
        return ar;
    }

    @POST
    @At
    @AdaptBy(type = UploadAdaptor.class, args = { "ioc:uploadImg" })
    @SLog(module = Constants.MODULE_SHOP, type=Constants.OPERATE_TYPE_UPDATE, content="更新【${args[0].rewardName}】奖品信息")
    public AjaxReturn update(@Param("..") Reward reward, @Param("rewardImg")TempFile rewardImg) {
        AjaxReturn ar = Ajax.ok();

        uploadRewardImg(reward, rewardImg);

        boolean flag = rwdInfoService.update(reward);
        if (!flag) {
            ar.setOk(false);
            ar.setMsg("Update reward info failure!!!");
        }
        return ar;
    }

    private void uploadRewardImg(Reward reward, TempFile rewImg) {
        if (rewImg != null) {
            File rwdImgFile = rewImg.getFile();
            String newImgName = Utils.renameUploadFile(rwdImgFile.getName());
            String imgPath = new StringBuffer(props.get(Constants.UPLOAD_ATTACHMENT_SAVE_PATH))
                    .append(props.get(Constants.REWARD_IMG_FOLDER)).append("/").append(newImgName).toString();
            String imgURL = Constants.RWD_IMG_CTX_PATH + newImgName;
            logger.infof("Reward image display context url: %s", imgURL);
            reward.setRewardImg(imgURL);

            try {
                Files.copyFile(rwdImgFile, new File(imgPath));
            } catch (IOException e) {
                logger.error("Upload reward image fail!!!", e);
            }
        }
    }

    @POST
    @At
    public AjaxReturn delete(@Param("rwdId") Integer rwdId) {
        AjaxReturn ar = Ajax.ok();
        Reward reward = new Reward();
        reward.setRewardId(rwdId);
        reward.setDataStatus("1");
        try {
            boolean flag = rwdInfoService.update(reward);
            ar.setOk(flag);
        } catch (Exception e) {
            logger.error("Delete reward info fail!!!", e);
        }
        return ar;
    }

    @POST
    @At
    public Map<String, Object> dataList(int page, int rows) {
        logger.infof("enter reward info: %d,%d", page, rows);
        return rwdInfoService.getDataList(page, rows);
    }
}
